
import SystemDataSys from "./SystemDataSys";
import frameworkManager from "./frameworkManager";
import ClientData from "./ClientData";
import SdkHelper from "./SdkHelper";
import HttpUtil from "./HttpUtil";
import TimeUtils from "./TimeUtils";
import UrlMgr from "./UrlMgr";

class Service {
  static genCommonRequestData() {
    return ClientData.genFormData();
  }

  static getRequestData(data) {
    console.log("getRequestData data: ", JSON.stringify(data));
    if (!SdkHelper.bd_did) {
      // SdkHelper.initBD(SdkHelper.getBD_did() || "");
    }
    let requestData = Object.assign({}, data);
    requestData.dev_token = SdkHelper.bd_did;
    let commonRequestData = this.genCommonRequestData();
    if (requestData) {
      // if (SystemDataSys.encrypt) {
      //   requestData = SdkHelper.getAesEncrypData(JSON.stringify(requestData));
      // }
      // requestData = JSON.stringify(requestData);
      // commonRequestData.append("business_data", requestData);
    }
    return commonRequestData;
  }

  static genSign(e, t, a) {
    const o = [
      "version_name",
      "channel_name",
      "device_id",
      "time",
    ];
    let i = `/${UrlMgr.getInstance().getUri(e)}`;
    for (let r = 0; r < o.length; r++) {
      const l = o[r];
      if ("time" == l) {
        i += ` ${t}`;
      } else {
        const s = ClientData.getAttr(l);
        i += "" != s && null != s ? ` ${s}` : " null";
      }
    }
    i += ` ${a}`;
    i += " gohell";
    const c = CryptoJS.enc.Base64.stringify(CryptoJS.MD5(i));
    return c
      .replace(new RegExp("\\+", "g"), "-")
      .replace(new RegExp("/", "g"), "_")
      .replace(new RegExp("=", "g"), "");
  }

  static uuid() {
    const e = [];
    for (let t = 0; t < 36; t++) {
      e[t] = "0123456789abcdef".substr(
        Math.floor(16 * Math.random()),
        1
      );
    }
    e[14] = "4";
    e[19] = "0123456789abcdef".substr((3 & e[19]) | 8, 1);
    e[8] = e[13] = e[18] = e[23] = "-";
    return e.join("");
  }

  static getCommonUrlData(t) {
    // const a = SdkHelper.getUrlSplicingString();
    // const o = `/${UrlMgr.getInstance().getUri(t)}`;
    // if (null != a) {
    //   const i = TimeUtils.getTimeinSeconds() + 120;
    //   const r = this.uuid();
    //   a += `&nonce_str=${r}&et=${i}&ngister=${SdkHelper.getNgister(
    //     o,
    //     i,
    //     r
    //   )}`;
    // }
    return {};
  }

  static genRequestUrl(t) {
    const a = UrlMgr.getInstance().getUrl(t);
    const o = this.getCommonUrlData(t);
    return o ? `${a}?${o}` : a;
  }

  static request(t) {
    frameworkManager.log(
      "请求参数========",
      this.getRequestData(t.getRequestData())
    );
    HttpUtil.queuePost(
      this.genRequestUrl(t.getRequestType()),
      this.getRequestData(t.getRequestData()),
      t
    );
  }
}

export default Service;